/*
 *  Copyright (c) 2014 James Buncle
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 * 
 */
package com.jbuncle.seleniumiderunner;

import static com.jbuncle.seleniumiderunner.Utils.getTitle;
import static com.jbuncle.seleniumiderunner.Utils.readFile;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author James Buncle
 */
public class TestCase {

    private final Collection<Test> tests;
    private final URL baseurl;
    private final String name;

    public TestCase(final File file, final String name) throws IOException {
        final Document htmlDocument = Jsoup.parse(readFile(file));
        this.tests = getCommands(htmlDocument);
        this.baseurl = getBaseURL(htmlDocument);
        this.name = name;
    }

    public TestCase(final File file) throws IOException {
        final Document htmlDocument = Jsoup.parse(readFile(file));
        this.tests = getCommands(htmlDocument);
        this.baseurl = getBaseURL(htmlDocument);
        this.name = getTitle(htmlDocument);
    }

    public String getName() {
        return name;
    }

    public Iterable<Test> getTests() {
        return new LinkedList<>(tests);
    }

    private static Collection<Test> getCommands(final Document htmlDocument) throws IOException {
        final Elements links = htmlDocument.select("table tbody tr");
        final Collection<Test> commands = new LinkedList<>();
        for (final Element tr : links) {
            final String action = tr.child(0).text();
            final String target = tr.child(1).text();
            final String value = tr.child(2).text();
            commands.add(new Test(target, action, value));
        }
        return commands;
    }

    public void run(final List<TestDriver> testDrivers) {
        //Set base urls
        for (final TestDriver testDriver : testDrivers) {
            testDriver.setBaseURL(this.baseurl);
        }
        for (final Test test : this.tests) {
            
            try {
                test.run(testDrivers);
            } catch (InvocationTargetException ex) {
                throw new RuntimeException(ex.getCause());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    private static URL getBaseURL(final Document htmlDocument) throws MalformedURLException {
        final Elements links = htmlDocument.select("head > link");
        return new URL(links.get(0).attr("href"));
    }

}
